<?php
header("Content-type:text/html;charset=utf-8");
// 设置数据库的链接和数据库名
$dsn = "mysql:host=localhost;dbname=hdc50";
//账号
$username = "root";
//密码
$password = "";

// 错误机制
try {
	$pdo = new Pdo($dsn,$username,$password);
} catch (PDOException $e) {
	//错误提示
	die($e->getMessage());
}

// 设置编码 utf8

 $pdo->query("set names utf8");

$data = ['username'=>'小名','age'=>'1998-3-4','sex'=>1];

try {
	$sql = "insert into stu(username,age,sex) values(?,?,?)";
	//准备要执行的查询
	$state = $pdo->prepare($sql);
	$id=0;
	foreach ($data as $key => $value) {
		// print_r($data); 判断value是否是数字或字符串
		$style = is_numeric($value)?PDO::PARAM_INT:PDO::PARAM_STR;
		$$key=$value;
		//绑定参数
		$state->bindParam(++$id,$$key,$style);
	}
	//负责执行准备好的查询
	$row = $state->execute();
	
} catch (PDOException $e) {
	$e->getMessage();
}





 ?>